package com.czxy.tanhua.dubbo.api;

import com.czxy.tanhua.dubbo.contants.CommentType;
import com.czxy.tanhua.dubbo.mongoapi.CommentApi;
import com.czxy.tanhua.mongo.Comment;
import com.czxy.tanhua.mongo.Movement;
import com.czxy.tanhua.vo.CommentTdo;
import com.czxy.tanhua.vo.PublicPageVo;
import com.czxy.tanhua.vo.ResultList;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/4/12 22:13
 */
@DubboService
public class CommentApiImpl implements CommentApi {
    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public ResultList findComments(ObjectId movementId, PublicPageVo publicPageVo) {
        ResultList<Comment> objectResultList = new ResultList<>();
        //拼凑条件
        Query query = new Query();
        //添加条件
        query.addCriteria(Criteria.where("publishId").is(movementId));

        long count = mongoTemplate.count(query, Comment.class);
        query.limit(publicPageVo.getPagesize().intValue()).skip((publicPageVo.getPage()-1)*publicPageVo.getPagesize()).with(Sort.by(Sort.Order.desc("created")));

        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        objectResultList.setItems(comments);
        objectResultList.setCounts(Integer.valueOf(count+""));
        objectResultList.setPagesize(publicPageVo.getPagesize().intValue());
        objectResultList.setPage(publicPageVo.getPage().intValue());
        objectResultList.setPages((int) (count%publicPageVo.getPagesize() == 0 ? count/ publicPageVo.getPagesize():(count/ publicPageVo.getPagesize())+1));
        //返回处理的数据
        return objectResultList;
    }

    @Override
    public void addcomment(CommentTdo commentTdo, Long userId) {
        //通过动态评论id查询到指定的动态
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(commentTdo.getMovementId()));
        Movement movement = mongoTemplate.findOne(query, Movement.class);
        System.out.println(movement);
        //保存新的评论
        Comment comment = new Comment();;
        comment.setCommentType(CommentType.COMMENT.getType());
        comment.setPublishId(movement.getId());
        comment.setContent(commentTdo.getComment());
        comment.setUserId(userId);
        comment.setPublishUserId(movement.getUserId());
        comment.setCreated(new Date().getTime());
        comment.setLikeCount(0);

        //存储数据
        mongoTemplate.save(comment,"comment");

        //修改当前动态的评论数量
        Integer commentCount = movement.getCommentCount();
        movement.setCommentCount(commentCount+1);
        Query query1 = new Query();
        query1.addCriteria(Criteria.where("id").is(commentTdo.getMovementId()));
        Update update = new Update().set("commentCount",commentCount+1);
        mongoTemplate.updateFirst(query1,update,Movement.class);
    }
}
